<?php

namespace App\Models\CacheModel\wecaht;

use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class WechatBlessRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.wechat_bless_record';

    protected $cacheModel = 'wechat_h5.bless';

    /**
     * 新增 信息
     * @param array $data
     * @return int
     */
    public function SaveData(array $data)
    {
        try
        {
            $id = $this->insertGetId($data);
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return 0;
        }

        return (int)$id;
    }

    /**
     * 更新 信息
     * @param int $id
     * @param int $uid
     * @param int $date
     * @param array $data
     * @return bool
     */
    public function UpdateData(int $id, int $uid, int $date, array $data)
    {
        try
        {
            $this->where(['id' => $id])->update($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, "{$uid}:{$date}")->setCache($data);
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }

        return true;
    }

    /**
     * 获取 祈福 信息
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public function getBlessInfo(int $uid)
    {
        $date = (int)(date('Ymd'));
        $resData = CacheManage::getInstance()->setCacheConfig($this->cacheModel, "{$uid}:{$date}")
            ->getDataAndSet(function () use ($uid, $date)
            {
                try
                {
                    $data = $this->where(['uid' => $uid, 'date' => $date])->findOrFail()->toArray();

                    return $data;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            });

        if ($resData)
        {
            $resData = $this->dataInit($resData);
        } else
        {
            $resData = [
                'id' => 0,
                'uid' => $uid,
                'date' => $date,
                'bless1' => 0,
                'bless1_time' => 0,
                'bless2' => 0,
                'bless2_time' => 0,
                'bless3' => 0,
                'bless3_time' => 0,
                'create_time' => 0,
            ];
        }

        return $resData;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $index => $itemData)
        {
            $resData[$index] = $this->dataInit($itemData);
        }

        return $resData;
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        return arrayKeyData2int($data, ['id', 'uid', 'date', 'bless1', 'bless1_time', 'bless2', 'bless2_time', 'bless3', 'bless3_time', 'create_time']);
    }


}
